import React, { useState, useEffect } from 'react';
import {
    Button,
    Cascader,
    DatePicker,
    Form,
    Input,
    InputNumber,
    Radio,
    Select,
    Switch,
    TreeSelect,
} from 'antd';
import type { FormProps } from 'antd';
import { useAppDispatch, useAppSelector } from '@/store/reactreduxts';
import type { noticeTS } from '@/store/crmsTS';
import { async_addnotice, async_updatenotice } from '@/store/noticeSlice';


interface Iprops {
    title: string
    handleOk: () => void
    itemobj: {} | noticeTS
}

const Myfrom: React.FC<Iprops> = (props) => {

    //获取数据
    let obj = useAppSelector(state => state.crms1)
    //console.log(obj);
    let dispatch = useAppDispatch()

    //数据回显
    const [form] = Form.useForm();
    useEffect(() => {
        form.setFieldsValue(props.itemobj)
    }, [])


    const onFinish: FormProps<noticeTS>['onFinish'] = (values) => {
        if (props.title == '添加') {
            //添加
            let obj = {
                ...values,
                "id": new Date().getTime()
            }
            //console.log(obj);
            //执行添加的方法
            dispatch(async_addnotice(obj))
        } else {
            //编辑
            let obj = {
                ...values,
                "id": props.itemobj.id
            }
            dispatch(async_updatenotice(obj))
        }
        props.handleOk()
    };

    return (
        <Form onFinish={onFinish} form={form}>

            <Form.Item label="公告标题" name='notice'
                rules={[{ required: true, message: 'Please input your username!' }]}>
                <Input />
            </Form.Item>

            <Form.Item label="公告类型" name='typeId'
                rules={[{ required: true, message: 'Please input your username!' }]}>
                <Select>
                    {
                        obj.typelist.map(item => {
                            return <Select.Option key={item.typeId} value={item.typeId}>
                                {item.label}
                            </Select.Option>
                        })
                    }
                </Select>
            </Form.Item>

            <Form.Item label="状态" name='collect'>
                <Radio.Group
                    options={[
                        { value: true, label: '正常' },
                        { value: false, label: '异常' },
                    ]}
                />
            </Form.Item>

            <Form.Item label="内容">
                <Input.TextArea placeholder='新版本内容' />
            </Form.Item>


            <Form.Item>
                <Button type='primary' htmlType="submit">完成</Button>
            </Form.Item>
        </Form>
    );
};

export default Myfrom;